import Vue from 'vue';
import VueRouter from 'vue-router';
import sousuo from '../views/sousuo.vue';
import store from '../store/index';
Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'home',

    // component:()=>import("../views/Dreame/index.vue")
    component: () => import('../views/Dreame/dreame.vue'),
  },
  {
    path: '/sousuo',
    name: 'sousuo',
    component: sousuo,
  },
  {
    path: '/reader/:id/:sort',
    params: {
      id: Number,
      sort: Number,
    },
    name: 'reader',
    component: () => import('../views/ReadView.vue'),
    meta: {
      bottom: true,
    },
  },

  // 导航页
  {
    path: '/novel',
    // path: "/novel/",
    name: 'novel',
    params: {
      id: Number,
      sort: Number,
    },

    meta: {
      bottom: true,
    },
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/synopsis2.vue'),
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/login.vue'),
  },

  {
    path: '/my',
    name: 'my',
    meta: { author: true },
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/user/my.vue'),
    // import(/* webpackChunkName: "about" */ "../views/mine.vue"),
  },
  {
    path: '/iSetUp',
    name: 'iSetUp',
    component: () =>
      import(/* webpackChunkName: "about" */ '../components/iSetUp/iSetUp.vue'),
  },

  // 书架
  {
    path: '/bookshelf',
    name: '书架',
    meta: { name: 'bookshelf' },
    component: () =>
      import(/* webpackChunkName: "sort" */ '../views/bookshelf/index.vue'),
    children: [
      {
        path: 'my',
        name: 'my books',
        meta: { name: 'my' },
        component: () =>
          import(/* webpackChunkName: "sort" */ '../views/bookshelf/my.vue'),
      },
      {
        path: 'history',
        name: 'footprint',
        meta: { name: 'history' },
        component: () =>
          import(
            /* webpackChunkName: "sort" */ '../views/bookshelf/history.vue'
          ),
      },
    ],
  },

  {
    path: '/dreame',
    name: 'dreame',
    component: () => import('../views/Dreame/dreame.vue'),
  },

  //我的
  {
    path: '/mine',
    name: 'mine',

    component: () =>
      import(/* webpackChunkName: "about" */ '../views/mine.vue'),
  },
  {
    path: '/store',
    name: 'store',
    meta: {
      ischile: true,
      title: 'Store',
    },
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/user/store.vue'),
  },
  {
    path: '/coins',
    name: 'coins',
    meta: {
      ischile: true,
      title: 'Coins',
    },
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/user/coins.vue'),
  },
  {
    path: '/gender',
    name: 'gender',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../components/iSetUp/gender.vue'),
  },
  {
    path: '/transaction',
    name: 'transaction',
    meta: {
      ischile: true,
      title: 'Coins',
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/user/transaction.vue'),
  },
  {
    path: '*',
    name: '404',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../components/iSetUp/gender.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  routes,
  // scrollBehavior(to, from, savedPosition) {
  //   // return 期望滚动到哪个的位置

  //   return {
  //     x: 0, y: 0,
  //   }
  // }
});

router.beforeEach((to, from, next) => {
  console.log('路由箭头', to.meta.ischile);

  store.commit('CHANGDE_BACK', !!to.meta.ischile);
  if (!!to.meta.ischile) {
    store.commit('CHANGE_TITLE', to.meta.title);
  }

  if (to.meta.bottom) {
    store.commit('CHANGE_HOMESHOW', to.meta.bottom);
  } else {
    store.commit('CHANGE_HOMESHOW', false);
  }
  console.log('路由拦截', to);
  if (to.meta.author && !localStorage.getItem('token')) {
    // next("/login")
    console.log('需要登录');
    next({ name: 'login' });
    return;
  } else {
    next();
  }
});

export default router;
